//
//  DXNetworkTools.swift
//  新浪微博
//
//  Created by dix on 2017/2/4.
//  Copyright © 2017年 dix. All rights reserved.
//

import UIKit
import AFNetworking

//请求方式的枚举(可以支持字符串类型)
enum DXNetworkToolsMethod : String{
    case get = "get"
    case post = "post"
}

class DXNetworkTools: AFHTTPSessionManager {
    //全局访问点
    static let shared : DXNetworkTools = {
        let tools = DXNetworkTools()
    tools.responseSerializer.acceptableContentTypes?.insert("text/html")
    tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    
    }()
    /// 网络请求公共方法
    ///
    /// - Parameters:
    ///   - method: 请求方式
    ///   - urlString: 请求url
    ///   - parameters: 请求参数
    ///   - success: 成功的闭包
    ///   - failure: 失败的闭包
    func request(method: DXNetworkToolsMethod, urlString: String, parameters: Any?, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        // 如果是get请求
        if method == .get {
            
            self.get(urlString, parameters: parameters, progress: nil, success: { (_, res) in
                // 执行闭包
                success(res)
            }, failure: { (_, err) in
                // 执行闭包
                failure(err)
            })
        }else {
            // post 请求
            self.post(urlString, parameters: parameters, progress: nil, success: { (_, res) in
                // 执行闭包
                success(res)
            }, failure: { (_, err) in
                // 执行闭包
                failure(err)
            })
        }
    }
}

extension DXNetworkTools{
    func loadHomeData(success : @escaping(Any?)->(), failure:@escaping (Error)->()){
        //请求url
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json";
        //请求参数
        let params = ["access_token":DXOAuthViewModel.shared.accessToken!]
        //发送请求
        request(method: DXNetworkToolsMethod.get, urlString: urlStr, parameters: params, success: success, failure: failure)
    }
}
//MARK: - OAuth网络请求
extension DXNetworkTools{
    //请求获取accessToken
    func loadUserAccount(code : String, success : @escaping(Any?)->(), failure:@escaping (Error)->()){
        // 请求的url
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id": APPKEY,
            "client_secret": APPSECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": REDIRECT_URI
        ]
        // 发送请求
        request(method: DXNetworkToolsMethod.post, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    // 获取用户信息
    func loadUserInfo(userAccountModel : DXUserAccountModel, success : @escaping(Any?)->(), failure:@escaping (Error)->()){
        //请求的url
        let urlStr = "https://api.weibo.com/2/users/show.json"
        //请求的参数
        let params = [
            "access_token" : userAccountModel.access_token!,
            "uid" : userAccountModel.uid!
        ]
        //发送请求
        request(method: DXNetworkToolsMethod.get, urlString: urlStr, parameters: params, success: success, failure: failure)
    }
}
